<?php
  auth(ADMIN,false,false);

	$action = (isset($_GET["action"])) ? $_GET["action"] : "index";
	
		switch($action){
			case "index" :
				echo '<div id="container_4" style="display:none">
													  <div class="sous_contenu_membre">Administration de la FAQ';
				echo '<a href="'.getLienModule("faqs","faqs.admin").'?action=addfaq">Ajouter des Q/R dans la FAQ</a><br />';
				echo '<a href="'.getLienModule("faqs","faqs.admin").'?action=listfaq">Modifier/Supprimer des Q/R dans la FAQ</a></div></div>';
				//echo '<a href="'.getLienModule("faqs","faqs.admin").'?action=listfaq">Supprimer des Q/R dans la FAQ</a>';
			break;	
			
			case "addfaq" :
				if(!isset($_POST['ask'],$_POST['answer']))
				{
					$type = 'bbcode_courant';
					$_tab['action'] = $_SERVER["REQUEST_URI"];
					$_tab['other'] = 'Question :<input size="70%;" type="text" value="" id="bbcode_ask" name="ask">
					';
					
					$_tab['textareaName'] = 'answer';
					$_tab['textareaLabel'] = "Réponse";
					
					include "./".DIR_INCLUDE."/formulaire.php";
						$_tab = '';
				}else{
					
					if(strlen($_POST['ask'])<3 or strlen($_POST['answer'])<3){
						redirect("Votre question et votre réponse doivent comporter au minimum 3 caractères !",$_SERVER["REQUEST_URI"]);
					}else{
						$req = $pdo->prepare("INSERT INTO forum_faqs(faq_ask,faq_answer) VALUES(?,?)");
						$req->execute(array($_POST['ask'],$_POST['answer']));
						
						redirect("Votre Q/R à bien été ajouté à la FAQ",getLienModule("admin","index.admin"));
					}
				}
			break;
			
			case "listfaq" :
				echo '<div class="cadre_transparent">
				<div class="panel_titre_question_faqs">Question</div>
				<div class="panel_titre_action_faqs">Action</div>
						</div>';
						
				$req = $pdo->query("SELECT * FROM forum_faqs");
					if($req->rowCount() > 0)
					{
						echo '<div class="cadre_visible">
				             
							 <div class="cadre_titre">Les Faqs</div>';
						while($data = $req->fetch(PDO::FETCH_OBJ)){
							echo '
							      <div class="cadre_border_bottom">
				<div class="panel_contenu_question_faqs">'.$data->faq_ask.'</div>
				<div class="panel_contenu_action_faqs"><a href="'.getLienModule("faqs","faqs.admin").'?action=updatefaq&faqid='.$data->faq_id.'">Modifier</a> <a href="'.getLienModule("faqs","faqs.admin").'?action=deletefaq&faqid='.$data->faq_id.'">Supprimer</a></div>
								  </div>';	
						}
					echo '</div>';
					echo $endScript = '<script>
						
							;(function(){
								
								$("fieldset .cadre_border_bottom:last-child").attr("class","cadre_border_nobottom");
								
							})();
						
						</script>';
					
					}else{
						echo '<div class="cadre_transparent">Aucune Q/R dans la FAQ pour le moment !</div>';	
					}
					
			break;
			
			case "updatefaq" :
				if(isset($_GET["faqid"]))
				{
					$req = $pdo->prepare("SELECT * FROM forum_faqs WHERE faq_id=?");
					$req->execute(array($_GET["faqid"]));
					if($req->rowCount() > 0)
					{
						$data = $req->fetch(PDO::FETCH_OBJ);
						if(!isset($_POST['ask'],$_POST['answer']))
						{
							$type = 'bbcode_courant';
							$_tab['action'] = $_SERVER["REQUEST_URI"];
							$_tab['other'] = '



Question :
												<input size="70%;" type="text" id="bbcode_ask" name="ask" value="'.$data->faq_ask.'">
							';
							
							$_tab['textareaName'] = 'answer';
							$_tab['textareaLabel'] = "Réponse";
							$_tab["textareaValue"] = $data->faq_answer;
							include "./".DIR_INCLUDE."/formulaire.php";
						}else{
							if(strlen($_POST['ask'])<3 or strlen($_POST['answer'])<3){
								redirect("Votre question et votre réponse doivent comporter au minimum 3 caractères !",$_SERVER["REQUEST_URI"]);
							}else{
								$req = $pdo->prepare("UPDATE forum_faqs SET faq_ask=?,faq_answer=? WHERE faq_id=?");
								$req->execute(array($_POST['ask'],$_POST['answer'],$_GET["faqid"]));
								redirect("FAQ : Modifié !",getLienModule("faqs","faqs.admin").'?action=listfaq');
							}
						}
					}else
						redirect("FAQ : Q/R innexistante",getLienModule("admin","index.admin"));	
				}else{
					redirect("Problème technique",getLienModule("admin","index.admin"));	
				}
			break;
			
			case "deletefaq" :
				if(isset($_GET["faqid"]))
				{
					$req = $pdo->prepare("SELECT * FROM forum_faqs WHERE faq_id=?");
					$req->execute(array($_GET["faqid"]));
					if($req->rowCount() > 0)
					{
						$data = $req->fetch(PDO::FETCH_OBJ);
								$req = $pdo->prepare("DELETE FROM forum_faqs WHERE faq_id=?");
								$req->execute(array($_GET["faqid"]));
								redirect("FAQ : Q/R supprimé !",getLienModule("faqs","faqs.admin").'?action=listfaq');
					}else
						redirect("FAQ : Q/R innexistante",getLienModule("admin","index.admin"));	
				}else{
					redirect("Problème technique",getLienModule("admin","index.admin"));	
				}
			break;
		}

?>